﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using System.Data;


namespace CslaHispano.EncapsulatedInvoke.Domain.Entities
{
    public partial class ModuleEdit
    {
        #region Methods

        private void LoadChilds()
        {
            Options = DataPortal.FetchChild<ModuleOptions>(ModuleId);
        }

        private void UpdateChilds()
        {
            DataPortal.UpdateChild(Options, this);
        }

        private void CreateChilds()
        {
            Options = DataPortal.CreateChild<ModuleOptions>();
        }

        #endregion

        #region Data Portal

        protected override void Child_Create()
        {
            using (BypassPropertyChecks)
            {
                ModuleId = Guid.NewGuid().ToString();
                Status = true;                
            }
            
            CreateChilds();
            base.Child_Create();
        }

        private void Child_Fetch(IDataReader data)
        {
            using (BypassPropertyChecks)
            {
                ModuleId = data.GetString(data.GetOrdinal("ModuleId"));
                SystemId = data.GetString(data.GetOrdinal("SystemId"));
                Code = data.GetString(data.GetOrdinal("Code"));
                Description = data.GetString(data.GetOrdinal("Description"));
                Status = data.GetBoolean(data.GetOrdinal("Status"));
            }
            LoadChilds();
        }

        private void Child_Insert(SystemEdit system)
        {
            //using (BypassPropertyChecks)
            //{
            //    var dal = IoCFactory.Instance.CurrentContainer.Resolve<ISystemModuleRepository>();

            //    using (BypassPropertyChecks)
            //    {
            //        dal.Insert(ModuleId, system.SystemId, Code, Description, Status);
            //    }
            //    UpdateChilds();
            //    dal = null;
            //}
        }

        private void Child_Update(SystemEdit system)
        {
            //using (BypassPropertyChecks)
            //{
            //    var dal = IoCFactory.Instance.CurrentContainer.Resolve<ISystemModuleRepository>();

            //    using (BypassPropertyChecks)
            //    {
            //        dal.Update(ModuleId, system.SystemId, Code, Description, Status);
            //    }
            //    FieldManager.UpdateChildren(this);
            //    dal = null;
            //}
        }

        private void Child_DeleteSelf(SystemEdit system)
        {
            //using (BypassPropertyChecks)
            //{
            //    var dal = IoCFactory.Instance.CurrentContainer.Resolve<ISystemModuleRepository>();

            //    using (BypassPropertyChecks)
            //    {
            //        dal.Delete(ModuleId);
            //    }
            //    CreateChilds();
            //    dal = null;
            //}
        }

        #endregion
    }
}
